Imports System.Math
Imports System.Drawing.Drawing2D


Module modCommon

    Public Const c_Demo As Boolean = True
    Public Const c_GameSystemVersion As String = "0.2.3"
    Public Const c_IconHalfWidth = 16
    Public Const c_IconHalfHeight = 16
    Public Const c_Module = "modCampaignerCommon"

    Public Function XYDistance(ByVal X1, ByVal X2, ByVal Y1, ByVal Y2) As Integer
        'returns the distance between two 2d coordinates
        XYDistance = Int(Abs(Math.Sqrt(((X1 - X2) * (X1 - X2)) + ((Y1 - Y2) * (Y1 - Y2)))))

    End Function

    Public Function XYDistance(ByVal pnt1 As Point, ByVal pnt2 As Point) As Int16

    End Function


    Public Sub DrawZOC(ByVal gr As Graphics, ByVal pnt As Point, ByVal radius As Int16, Optional ByVal enemy As Boolean = False)

        If radius = 0 Then Exit Sub

        pnt.X -= radius / 2
        pnt.Y -= radius / 2

        If enemy Then
            gr.DrawEllipse(New Pen(Color.FromArgb(255, 255, 75, 75)), pnt.X, pnt.Y, radius, radius)
            gr.FillEllipse(New SolidBrush(Color.FromArgb(100, 255, 75, 75)), pnt.X, pnt.Y, radius, radius)

        Else
            gr.DrawEllipse(New Pen(Color.FromArgb(255, 75, 255, 75)), pnt.X, pnt.Y, radius, radius)
            gr.FillEllipse(New SolidBrush(Color.FromArgb(100, 75, 255, 75)), pnt.X, pnt.Y, radius, radius)
        End If

    End Sub

    Public Sub DrawBorderedCircle(ByVal gr As Graphics, ByVal pnt As Point, ByVal radius As Int16, ByVal outterColor As Color, ByVal innerColor As Color)
        Dim outter As New Pen(outterColor)
        Dim inner As New Pen(innerColor)

        pnt.X -= radius / 2
        pnt.Y -= radius / 2

        gr.DrawEllipse(outter, pnt.X, pnt.Y, radius, radius)
        gr.DrawEllipse(inner, pnt.X + 1, pnt.Y + 1, radius - 2, radius - 2)
        gr.DrawEllipse(outter, pnt.X + 2, pnt.Y + 2, radius - 4, radius - 4)


    End Sub



#Region "Add Objects"

    Public Sub AddObjectiveType(ByRef ptrCGD As cmplib01.CampaignerGameData, ByVal mClassTypes() As String)
        If ptrCGD.TheaterMapFilename = "none" Then
            MsgBox("You must select a theater map before adding ground chits.", MsgBoxStyle.Information, "Add Theater Map")
            Exit Sub
        End If

        Dim newItem As New cmplib01.ObjectiveType()
        Dim path As String
        Dim basePath As String = System.Reflection.Assembly.GetExecutingAssembly.Location()
        Dim baseType As String

        basePath = basePath.Replace("Campaigner.exe", "icons\")

        ' System.Reflection.Assembly.GetExecutingAssembly.Location()  <-- replaces app.path
        Try
            ' set up the new icons for this guy...
            path = basePath + "basicobjective.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.Owner) = Bitmap.FromFile(path)

            path = basePath + "basicobjectiveenemy.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.Enemy) = Bitmap.FromFile(path)

            path = basePath + "basicobjectivesamefaction.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.SameFaction) = Bitmap.FromFile(path)

        Catch ex As Exception
            newItem.Icon(cmplib01.IconImageIndex_cgd.Owner) = Nothing
            newItem.Icon(cmplib01.IconImageIndex_cgd.Enemy) = Nothing
            newItem.Icon(cmplib01.IconImageIndex_cgd.SameFaction) = Nothing
        End Try

        ' set the name of the new succa
        newItem.Description = "New Objective Type"
        newItem.TypeName = "New Objective Type"
        newItem.Name = "New Objective Type"
        newItem.GroundChitClass = cmplib01.GroundChitClass_cgd.Objective

        Dim frmAddChit As New frmChitProperties(frmChitProperties.OpenMode.ObjectiveType, ptrCGD, newItem, mClassTypes)

        frmAddChit.ShowDialog()

        ' add the edited chit to the collection
        Try
            ' this is a loop that will attempt to add a new item to the current collection. If the keys are busted (ie same id) the CID is boosted
            ' by 100. This continues until the CID is an acceptable value.
            Dim i As Int16
            Dim Done As Boolean
            Do Until Done
                Try
                    ptrCGD.ObjectiveTypes.Add(newItem.Key, newItem)
                    Done = True
                Catch ex As SystemException
                    newItem.BoostMCID()
                    Done = False
                    Exit Try
                Catch ex As Exception
                    Exit Do
                End Try
            Loop
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Public Sub AddUnitType(ByRef ptrCGD As cmplib01.CampaignerGameData, ByVal mClassTypes() As String)
        If ptrCGD.TheaterMapFilename = "none" Then
            MsgBox("You must select a theater map before adding ground chits.", MsgBoxStyle.Information, "Add Theater Map")
            Exit Sub
        End If

        Dim newItem As New cmplib01.UnitType()
        Dim path As String
        Dim basePath As String = System.Reflection.Assembly.GetExecutingAssembly.Location()
        Dim baseType As String

        basePath = basePath.Replace("Campaigner.exe", "icons\")

        ' System.Reflection.Assembly.GetExecutingAssembly.Location()  <-- replaces app.path
        Try
            ' set up the new icons for this guy...
            path = basePath + "basicunit.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.Owner) = Bitmap.FromFile(path)

            path = basePath + "basicunitenemy.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.Enemy) = Bitmap.FromFile(path)

            path = basePath + "basicunitsamefaction.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.SameFaction) = Bitmap.FromFile(path)

        Catch ex As Exception
            newItem.Icon(cmplib01.IconImageIndex_cgd.Owner) = Nothing
            newItem.Icon(cmplib01.IconImageIndex_cgd.Enemy) = Nothing
            newItem.Icon(cmplib01.IconImageIndex_cgd.SameFaction) = Nothing
            Debug.WriteLine("!!! Error in opening file for icons: " & ex.Message.ToString)
        End Try

        newItem.Description = "New Unit Type"
        newItem.TypeName = "New Unit Type"
        newItem.Name = "New Unit Type"
        newItem.GroundChitClass = cmplib01.GroundChitClass_cgd.Unit

        Dim frmAddChit As New frmChitProperties(frmChitProperties.OpenMode.UnitType, ptrCGD, newItem, mClassTypes)

        frmAddChit.ShowDialog()

        ' add edited unit to collection
        Try
            ' this is a loop that will attempt to add a new item to the current collection. If the keys are busted (ie same id) the CID is boosted
            ' by 100. This continues until the CID is an acceptable value.
            Dim i As Int16
            Dim Done As Boolean
            Do Until Done
                Try
                    ptrCGD.UnitTypes.Add(newItem.Key, newItem)
                    Done = True
                Catch ex As SystemException
                    newItem.BoostMCID()
                    Done = False
                    Exit Try
                Catch ex As Exception
                    Exit Do
                End Try
            Loop
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Public Sub AddTokenType(ByRef ptrCGD As cmplib01.CampaignerGameData, ByVal mClassTypes() As String)
        If ptrCGD.TheaterMapFilename = "none" Then
            MsgBox("You must select a theater map before adding ground chits.", MsgBoxStyle.Information, "Add Theater Map")
            Exit Sub
        End If

        If ptrCGD.RuleTypes_g.Count = 0 Then
            MsgBox("There must be at least one rule defined before you may create token types.", MsgBoxStyle.Information, "Define Rules")
            Exit Sub
        End If

        Dim newItem As New cmplib01.TokenType()
        Dim path As String
        Dim basePath As String = System.Reflection.Assembly.GetExecutingAssembly.Location()
        Dim baseType As String

        basePath = basePath.Replace("Campaigner.exe", "icons\")

        Try
            ' set up default icons
            path = basePath + "basictoken.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.Owner) = Bitmap.FromFile(path)
            path = basePath + "basictokenenemy.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.Enemy) = Bitmap.FromFile(path)
            path = basePath + "basictokensamefaction.bmp"
            newItem.Icon(cmplib01.IconImageIndex_cgd.SameFaction) = Bitmap.FromFile(path)
        Catch ex As Exception
            newItem.Icon(cmplib01.IconImageIndex_cgd.Owner) = Nothing
            newItem.Icon(cmplib01.IconImageIndex_cgd.Enemy) = Nothing
            newItem.Icon(cmplib01.IconImageIndex_cgd.SameFaction) = Nothing
            Debug.WriteLine("!!! Error in opening file for icons: " & ex.Message.ToString)
        End Try
        newItem.MovementClassID = 2
        newItem.Description = "New Token Type"
        newItem.TypeName = "New Token Type"
        newItem.Name = "New Token Type"
        newItem.GroundChitClass = cmplib01.GroundChitClass_cgd.Token
        ' when adding a new token, we assign the first one found in the rules to the token, okay, so its an ugly hack.
        Dim de As DictionaryEntry
        Dim sKey As String

        For Each de In ptrCGD.RuleTypes_g.HT
            sKey = de.Key.ToString
            newItem.TokenRule.ruleTypeID = ptrCGD.RuleTypes_g.Item(sKey).RuleTypeID
            newItem.TokenRule.ruleName = ptrCGD.RuleTypes_g.Item(sKey).RuleName
            newItem.TokenRule.currentUses = ptrCGD.RuleTypes_g.Item(sKey).MaxUses
            Exit For
        Next

        Dim frmAddChit As New frmChitProperties(frmChitProperties.OpenMode.TokenType, ptrCGD, newItem, mClassTypes)
        frmAddChit.ShowDialog()

        Try
            ' this is a loop that will attempt to add a new item to the current collection. If the keys are busted (ie same id) the CID is boosted
            ' by 100. This continues until the CID is an acceptable value.
            Dim i As Int16
            Dim Done As Boolean
            Do Until Done
                Try
                    ptrCGD.TokenTypes.Add(newItem.Key, newItem)
                    Done = True
                Catch ex As SystemException
                    newItem.BoostMCID()
                    Done = False
                    Exit Try
                Catch ex As Exception
                    Exit Do
                End Try
            Loop
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Public Sub AddRule(ByRef ptrCGD As cmplib01.CampaignerGameData)
        Dim newRuleType As New cmplib01.RuleType()
        Dim fNewRule As New frmRulePicker(ptrCGD, newRuleType)

        fNewRule.ShowDialog()

        If Not fNewRule.CancelPressed Then

            Try
                ' this is a loop that will attempt to add a new item to the current collection. If the keys are busted (ie same id) the CID is boosted
                ' by 100. This continues until the CID is an acceptable value.
                Dim i As Int16
                Dim Done As Boolean
                Do Until Done
                    Try
                        ptrCGD.RuleTypes_g.Add(newRuleType, newRuleType.Key)
                        Done = True
                    Catch ex As SystemException
                        newRuleType.BoostMCID()
                        Done = False
                        Exit Try
                    Catch ex As Exception
                        Exit Do
                    End Try
                Loop
            Catch ex As Exception
                HandleException(c_Module, ex)
            End Try
        End If

    End Sub

    Public Sub AddResource(ByRef ptrCGD As cmplib01.CampaignerGameData)
        Dim newResourceType As New cmplib01.ResourceType()
        Dim fNewResource As New frmAddNewResourceType(newResourceType)

        fNewResource.ShowDialog()
        If Not fNewResource.CancelPressed Then
            ptrCGD.ResourceTypes_g.Add(newResourceType, newResourceType.Key)
        End If

    End Sub

    Public Sub AddOrder(ByRef ptrCGD As cmplib01.CampaignerGameData)
        Dim newOrder As New cmplib01.OrderType()
        Dim fNewOrder As New frmOrderType(newOrder)

        fNewOrder.ShowDialog()
        If Not fNewOrder.CancelPressed Then
            ' add the new order to the campaign game
            Try
                ' this is a loop that will attempt to add a new item to the current collection. If the keys are busted (ie same id) the CID is boosted
                ' by 100. This continues until the CID is an acceptable value.
                Dim i As Int16
                Dim Done As Boolean
                Do Until Done
                    Try
                        ptrCGD.Orders_g.Add(newOrder, newOrder.Key)
                        Done = True
                    Catch ex As SystemException
                        newOrder.BoostMCID()
                        Done = False
                        Exit Try
                    Catch ex As Exception
                        Exit Do
                    End Try
                Loop
            Catch ex As Exception
                HandleException(c_Module, ex)
            End Try
        End If

    End Sub

    Public Sub EditFactions(ByRef ptrCGD As cmplib01.CampaignerGameData)
        Dim fFact As New frmEditFactions(ptrCGD.Faction_g)

        fFact.ShowDialog()

    End Sub
#End Region

End Module

